package com.smartwebx.ha.web.util.json.module;

import org.apache.commons.lang3.BooleanUtils;

import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.google.common.primitives.Ints;

/**
 * Double 类型格式化
 * 
 * @author zhangxi
 * 
 */
public class DoubleModule extends SimpleModule {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	int scale = 1;
	boolean isNumber = false;

	public DoubleModule(Object scale) {
		this();
		this.scale = Ints.tryParse(String.valueOf(scale));
	}

	public DoubleModule(Object scale, Object isNumber) {
		this();
		this.scale = Ints.tryParse(String.valueOf(scale));
		this.isNumber = BooleanUtils.toBoolean(isNumber.toString());
	}

	public DoubleModule() {
		this("DoubleModule", Version.unknownVersion());
	}

	public DoubleModule(String name, Version version) {
		super(name, version);
	}

	@Override
	public void setupModule(SetupContext context) {
		super.addSerializer(Double.class, new DoubleSerializer(scale, isNumber));
		super.setupModule(context);
	}
}
